package com.example.uniapp.dao;

import android.annotation.SuppressLint;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.uniapp.bean.CookbookBean;
import com.example.uniapp.db.DBUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class CookbookDao {
    public static SQLiteDatabase db = DBUtils.con;

    /**
     * 查询所有食谱
     * @return
     */
    public static List<CookbookBean> getAllCookbookList() {
        List<CookbookBean> cookbookBeanList = new ArrayList<>();
        Cursor cursor = db.rawQuery("select * from cookbook", null);
        while(cursor.moveToNext()) {
            @SuppressLint("Range") String cookbookId = cursor.getString(cursor.getColumnIndex("cookbook_id"));
            @SuppressLint("Range") String userId = cursor.getString(cursor.getColumnIndex("user_id"));
            @SuppressLint("Range") String cookbookName = cursor.getString(cursor.getColumnIndex("cookbook_name"));
            @SuppressLint("Range") String cookbookReleaseTime = cursor.getString(cursor.getColumnIndex("cookbook_release_time"));
            @SuppressLint("Range") String cookbookImg = cursor.getString(cursor.getColumnIndex("cookbook_img"));
            CookbookBean cookbookBean = new CookbookBean(cookbookId, userId, cookbookName, cookbookReleaseTime, cookbookImg);
            cookbookBeanList.add(cookbookBean);
        }
        return cookbookBeanList;
    }

    /**
     * 根据食谱id查询食谱
     * @param id
     * @return
     */
    public static CookbookBean getCookbookListById(String id) {
        Cursor cursor = db.rawQuery("select * from cookbook where cookbook_id=?", new String[]{id});
        if(cursor.moveToNext()) {
            @SuppressLint("Range") String cookbookId = cursor.getString(cursor.getColumnIndex("cookbook_id"));
            @SuppressLint("Range") String userId = cursor.getString(cursor.getColumnIndex("user_id"));
            @SuppressLint("Range") String cookbookName = cursor.getString(cursor.getColumnIndex("cookbook_name"));
            @SuppressLint("Range") String cookbookReleaseTime = cursor.getString(cursor.getColumnIndex("cookbook_release_time"));
            @SuppressLint("Range") String cookbookImg = cursor.getString(cursor.getColumnIndex("cookbook_img"));
            return new CookbookBean(cookbookId, userId, cookbookName, cookbookReleaseTime, cookbookImg);
        }else {
            return null;
        }
    }

    /**
     * 根据userId获取用户发布的所有食谱信息
     * @param userId
     * @return
     */
    public static List<CookbookBean> getCookbookListByUserId(String userId) {
        List<CookbookBean> cookbookBeanList = new ArrayList<>();
        Cursor cursor = db.rawQuery("select * from cookbook where user_id=?", new String[]{userId});
        while(cursor.moveToNext()) {
            @SuppressLint("Range") String cookbookId = cursor.getString(cursor.getColumnIndex("cookbook_id"));
            @SuppressLint("Range") String cookbookName = cursor.getString(cursor.getColumnIndex("cookbook_name"));
            @SuppressLint("Range") String cookbookReleaseTime = cursor.getString(cursor.getColumnIndex("cookbook_release_time"));
            @SuppressLint("Range") String cookbookImg = cursor.getString(cursor.getColumnIndex("cookbook_img"));
            CookbookBean cookbookBean = new CookbookBean(cookbookId, userId, cookbookName, cookbookReleaseTime, cookbookImg);
            cookbookBeanList.add(cookbookBean);
        }
        return cookbookBeanList;
    }

    /**
     * 根据食谱名查询食谱
     * @param name
     * @return
     */
    public static List<CookbookBean> getCookbookListByName(String name) {
        String mohu_name = "%" + name + "%"; //实现模糊查询
        List<CookbookBean> cookbookBeanList = new ArrayList<>();
        Cursor cursor = db.rawQuery("select * from cookbook where cookbook_name like ?", new String[] {mohu_name});
        while(cursor.moveToNext()) {
            @SuppressLint("Range") String cookbookId = cursor.getString(cursor.getColumnIndex("cookbook_id"));
            @SuppressLint("Range") String userId = cursor.getString(cursor.getColumnIndex("user_id"));
            @SuppressLint("Range") String cookbookName = cursor.getString(cursor.getColumnIndex("cookbook_name"));
            @SuppressLint("Range") String cookbookReleaseTime = cursor.getString(cursor.getColumnIndex("cookbook_release_time"));
            @SuppressLint("Range") String cookbookImg = cursor.getString(cursor.getColumnIndex("cookbook_img"));
            CookbookBean cookbookBean = new CookbookBean(cookbookId, userId, cookbookName, cookbookReleaseTime, cookbookImg);
            cookbookBeanList.add(cookbookBean);
        }
        return cookbookBeanList;
    }

    /**
     * 添加食谱
     * @param userId
     * @param cookbookName
     * @param cookbookImg
     * @return
     */
    public static int addCookbook(String userId, String cookbookName, String cookbookImg) {
        // 获取当前的时间戳
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd-HH:mm:ss");
        String cookbookReleaseTime = now.format(formatter);

        //生成id
        String cookbookId = UUID.randomUUID().toString().replace("-", "");
        String[] data = {cookbookId, userId, cookbookName, cookbookReleaseTime, cookbookImg};

        //插入数据库
        try {
            db.execSQL("INSERT INTO cookbook (cookbook_id, user_id, cookbook_name, cookbook_release_time, cookbook_img)" +
                            "VALUES(?, ?, ?, ?, ?)",
                    data);
            return 1;
        }catch (Exception e) {
            return 0;
        }
    }

    /**
     * 删除食谱
     * @param cookbookId
     * @return
     */
    public static int delCookbook(String cookbookId) {
        try {
            db.execSQL("delete from cookbook where cookbook_id=?" ,
                    new String[] {cookbookId});
            return 1;
        }catch (Exception e) {
            return 0;
        }
    }

    /**
     * 更新食谱
     * @param cookbookId
     * @param cookbookName
     * @param cookbookImg
     * @return
     */
    public static int updateCookbook(String cookbookId, String cookbookName, String cookbookImg) {
        // 获取当前的时间戳
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd-HH:mm:ss");
        String cookbookReleaseTime = now.format(formatter);

        String[] data = {cookbookName, cookbookReleaseTime, cookbookImg, cookbookId};

        //插入数据库
        try {
            db.execSQL("update cookbook set cookbook_name=?, cookbook_release_time=?, cookbook_img=? where cookbook_id=?",
                    data);
            return 1;
        }catch (Exception e) {
            return 0;
        }
    }

    public static String getCookbookId(String cookbookName, String userId) {
        String[] data = {cookbookName, userId};
        try {
            Cursor cursor = db.rawQuery("select cookbook_id from cookbook where cookbook_name=? and user_id=?", data);
            if(cursor.moveToNext()) {
                @SuppressLint("Range") String cookbookId = cursor.getString(cursor.getColumnIndex("cookbook_id"));
                return cookbookId;
            }else return null;
        }catch (Exception e) {
            return null;
        }
    }
}
